package org.bigjava.controller;

import org.bigjava.bean.Garbage;
import org.bigjava.bean.Ljql;
import org.bigjava.biz.GarbageService;
import org.bigjava.biz.LjqlService;
import org.bigjava.common.Constant;
import org.bigjava.common.FenYe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LjqlController {
    @Autowired
    LjqlService ljqlService;
    @Autowired
    GarbageService garbageService;
    @Autowired
    HttpSession session;

    //根据站点名称或者清理时间搜索垃圾清理信息
    @RequestMapping("ljql/sousuo")
    public String ljqlSouSuo(String dyg,String deg){
        if(dyg==""){dyg = "0";}
        if(deg==""){deg = null;}
        int g_id  = Integer.parseInt(dyg);
        List<Ljql> lists = ljqlService.ljqlMoHu(g_id,deg);
        session.setAttribute("ljqls",lists);
        return "ljqlgl";
    }
    //删除垃圾清理信息
    @RequestMapping("ljql/shanchu")
    public String ljqlShanChu(int id){
        ljqlService.ljqlShanChu(id);
        Object a = session.getAttribute("yeshu");
        return "redirect:/ljql/fenye?a="+a;
    }
    //修改垃圾清理信息
    @RequestMapping("ljql/xiugai")
    public String ljqlXiuGai(Ljql ljql){
        Ljql ljqla = (Ljql) session.getAttribute("ljql");
        ljql.setId(ljqla.getId());
        ljqlService.ljqlXiuGai(ljql);
        return "redirect:/ljql/fenye";
    }
    //跳转到修改垃圾清理页面
    @RequestMapping("ljql/xg")
    public String ljqlxg(int id){
        Ljql ljql = ljqlService.ljqlId(id);
        session.setAttribute("ljql",ljql);
        List<Garbage> garbages = garbageService.garbageQuan();
        session.setAttribute("garbages",garbages);
        return "ljqlxg";
    }

    //添加垃圾清理信息
    @RequestMapping("ljql/tianjia")
    public String ljqlAdd(Ljql ljql){
        ljqlService.ljqlAdd(ljql);
        return "redirect:/ljql/fenye";
    }

    //跳转到垃圾清理添加页面(拿到站点列表)
    @RequestMapping("ljql/ljqltj")
    public String ljqltj(){
        List<Garbage> garbages = garbageService.garbageQuan();
        session.setAttribute("garbages",garbages);
        return "ljqltj";
    }
    //垃圾清理分页查询
    @RequestMapping("ljql/fenye")
    public String ljqlFenYe(String a){
        List<Garbage> garbages = garbageService.garbageQuan();
        session.setAttribute("garbages",garbages);
        //业主总人数
        int zong =  ljqlService.ljqlZong();
        session.setAttribute("zong",zong);
        //调用分页方法返回下一页开始id
        int ks = FenYe.fenYe(session,zong,a, Constant.fenye);
        //分页查询出的业主
        List<Ljql> lists = ljqlService.ljqlFenYe(ks,Constant.fenye);
        session.setAttribute("ljqls",lists);
        return "ljqlgl";
    }

}
